package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.ProductMapper;
import com.showcai.module.BaseModule;
import com.showcai.module.Order;
import com.showcai.module.Product;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.ProductService;
import com.showcai.service.ServiceBase;
import com.showcai.util.GenerateID;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-8-30.
 */
public class ProductServiceImpl extends ServiceBase implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Override
    public Product modifyProduct(Product product) throws MyRunTimeExcption {
        if (product.getTid()==null )
        {
            product.setProductId(GenerateID.CreatetID());
            product.setCreateTime(new Date());
            productMapper.insertProduct(product);
            return product;
        }
        productMapper.updateByPrimaryKey(product);
return product;
    }

    @Override
    public Product getProductByProductId(Long productId) throws MyRunTimeExcption {
        return productMapper.getProductByProductId(productId);
    }

    @Override
    public Product getProductByTid(Long tid) {
        return productMapper.getProductByTid(tid);
    }

    @Override
    public PageInfo<Product> getProductList(Integer page, Integer pageSize) throws MyRunTimeExcption {
        PageHelper.startPage(page, pageSize);
        List<Product> list = productMapper.getProductList();
        PageInfo<Product> pageinfo = new PageInfo<>(list);
        return pageinfo;
    }

    @Override
    public void stopProduct(Long productId) {
        Product product = new Product();
        product.setProductId(productId);
        product.setStatus(BaseModule.STATUS_UNVALID);
        productMapper.updateProductByPrimaryKeySelective(product);
    }

    @Override
    public void operateProduct(Long productId) {
        Product product = new Product();
        product.setProductId(productId);
        product.setStatus(BaseModule.STATUS_VALID);
        productMapper.updateProductByPrimaryKeySelective(product);
    }

    @Override
    public PageInfo getPageInfoProductList(Map<String, String> map) throws MyRunTimeExcption {
        return null;
    }
}
